<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use App\Models\Admin\Stats\Platform\Settlement\Day as StatsDay;
use App\Models\Admin\Stats\Platform\Settlement\Month as StatsMonth;
use App\Models\Admin\StatsTask\Platform\Settlement\Day as TaskDay;
use App\Models\Admin\StatsTask\Platform\Settlement\Month as TaskMonth;
use App\Core\Traits\Numeral;

class CreateStatsPlatformSettlementTable extends Migration
{
    use Numeral;

    /**
     * 统计任务表字段
     */
    private function makeStatsTask($tableName)
    {
        return function (Blueprint $table) use ($tableName) {
            $table->id();
            $table->unsignedInteger('timed');
            $table->string('plat_code', 64)->comment('平台code');

            $table->tinyInteger('attempts')->comment('任务尝试次数')->default(0);
            $table->tinyInteger('status')->comment('状态：1初始化，2推送到队列，3开始处理，4成功，5失败')->default(1)->index();

            $table->unsignedInteger('created');
            $table->unsignedInteger('updated');

            // 唯一索引
            $table->unique(['timed', 'plat_code'], "{$tableName}_unique");
        };
    }

    /**
     * 统计数据表字段
     */
    private function makeStats($tableName, $multiRate = false)
    {
        return function (Blueprint $table) use ($tableName, $multiRate) {
            $table->id();
            $table->unsignedInteger('timed');
            $table->string('plat_code', 64)->comment('平台code')->index();
            $table->string('currency', 16)->comment('币种code')->index();

            $table->decimal('bet_num', $this->decimals(), $this->precision())->comment('玩家投注')->default(0);
            $table->decimal('amount', $this->decimals(), $this->precision())->comment('平台收益')->default(0);

            $table->decimal('revenue', $this->decimals(), $this->precision())->comment('系统收益')->default(0);

            if (!$multiRate) {
                $table->decimal('rate', 6, 4)->comment('税收比例')->default(0);
            } else {
                $table->json('rates')->comment('税收比例');
            }

            $table->json('usd')->comment('换算USD');

            $table->unsignedInteger('created');
            $table->unsignedInteger('updated');

            // 唯一索引
            $table->unique(['timed', 'plat_code', 'currency'], "{$tableName}_unique");
        };
    }

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        /**
         * 统计任务表【天】
         */
        $tableName = TaskDay::tableName();
        // 创建表结构
        TaskDay::schemaConnection()->create($tableName, $this->makeStatsTask($tableName));
        // 表名注释
        TaskDay::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计任务--平台收益--天'");
        /**
         * 统计数据表【天】
         */
        $tableName = StatsDay::tableName();
        // 创建表结构
        StatsDay::schemaConnection()->create($tableName, $this->makeStats($tableName));
        // 表名注释
        StatsDay::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计--平台收益--天'");

        /**
         * 统计任务表【月】
         */
        $tableName = TaskMonth::tableName();
        // 创建表结构
        TaskMonth::schemaConnection()->create($tableName, $this->makeStatsTask($tableName));
        // 表名注释
        TaskMonth::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计任务--平台收益--月'");
        /**
         * 统计数据表【月】
         */
        $tableName = StatsMonth::tableName();
        // 创建表结构
        StatsMonth::schemaConnection()->create($tableName, $this->makeStats($tableName, true));
        // 表名注释
        StatsMonth::dbConnection()->statement("ALTER TABLE `{$tableName}` COMMENT '统计--平台收益--月'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // 统计任务表【天】
        TaskDay::schemaConnection()->dropIfExists(TaskDay::tableName());
        // 统计数据表【天】
        StatsDay::schemaConnection()->dropIfExists(StatsDay::tableName());

        // 统计任务表【月】
        TaskMonth::schemaConnection()->dropIfExists(TaskMonth::tableName());
        // 统计数据表【月】
        StatsMonth::schemaConnection()->dropIfExists(StatsMonth::tableName());
    }
}
